// components/uploadFile/index.js
var upload = require('../../utils/asyncUpload');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        showUploadEdit: {
            type: Boolean
        },
        type: {
            type: String
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        uploadOptions: [{
                name: '本地图片',
                type: 1,
                icon: 'https://pcsys.admin.ybc365.com/0b6f8f25-a176-4f53-83c2-6132135b9346'
            },
            {
                name: '本地视频',
                type: 2,
                icon: 'https://pcsys.admin.ybc365.com//a1767c1b-d2de-4ff2-b4bf-cf54845285c9'
            },
            {
                name: '微信音频',
                type: 3,
                icon: 'https://pcsys.admin.ybc365.com//b9192eb1-fbff-4901-9f8e-71ebb7977289'
            },
            {
                name: '微信文件',
                type: 4,
                icon: 'https://pcsys.admin.ybc365.com//e70a3042-a360-460c-9dab-edc71c883858'
            },
        ],
        circleValue: 0,
        gradientColor: {
            '0%': '#18BDF0',
            '100%': '#faebb1',
        },
        showCircle: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /** 过滤文件格式 */
        extname(filename) {
            if (!filename || typeof filename != 'string') {
                return false
            };
            let a = filename.split('').reverse().join('');
            let b = a.substring(0, a.search(/\./)).split('').reverse().join('');
            return b
        },
        /** 选择上传操作触发 */
        onSelectUpload(e) {
            switch (e.detail.type) {
                // 本地图片
                case 1:
                    wx.chooseMedia({
                        mediaType: ['image'],
                        count: 4,
                        success: (result) => {
                            let arr = []
                            let fileArr = result.tempFiles
                            let resourceType = "picture"
                            fileArr.forEach(item => {
                                arr.push(upload.upload(item.tempFilePath, (res) => {
                                    this.setData({
                                        circleValue: res,
                                        showCircle: true
                                    })
                                }));
                            })
                            Promise.all(arr).then((res) => {
                                fileArr.forEach((item, index) => {
                                    item.address = res[index];
                                    item.name = upload.mathRand() + '.png';
                                    item.type = this.properties.type;
                                    item.resourceType = resourceType
                                    delete item.fileType
                                    delete item.size
                                    delete item.tempFilePath
                                });
                                this.setData({
                                    circleValue: 0,
                                    showCircle: false,
                                })
                                this.triggerEvent('callSomeFun', fileArr)
                            });
                            this.setData({
                                showUploadEdit: false
                            })
                        }
                    })
                    break;
                case 2:
                    wx.chooseMedia({
                        mediaType: ['video'],
                        count: 1,
                        success: (result) => {

                            let arr = []
                            let fileArr = result.tempFiles
                            let resourceType = "video"
                            fileArr.forEach(item => {
                                arr.push(upload.upload(item.tempFilePath, (res) => {
                                    this.setData({
                                        circleValue: res,
                                        showCircle: true
                                    })
                                }));
                            })
                            Promise.all(arr).then((res) => {
                                fileArr.forEach((item, index) => {
                                    item.address = res[index];
                                    item.name = upload.mathRand() + '.mp4';
                                    item.type = this.properties.type;
                                    item.resourceType = resourceType
                                    delete item.fileType
                                    delete item.size
                                    delete item.thumbTempFilePath
                                    delete item.duration
                                    delete item.height
                                    delete item.width
                                    delete item.tempFilePath
                                });
                                this.setData({
                                    circleValue: 0,
                                    showCircle: false,
                                })
                                this.triggerEvent('callSomeFun', fileArr)
                            });
                            this.setData({
                                showUploadEdit: false
                            })
                        },
                        fail: (res) => {},
                        complete: (res) => {},
                    })
                    break;
                case 3:
                    wx.chooseMessageFile({
                        type: 'file',
                        count: 2,
                        extension: ['mp3'],
                        success: (result) => {
                            let arr = []
                            let fileArr = result.tempFiles
                            let resourceType = "audio"
                            fileArr.forEach(item => {
                                console.log(item);
                                arr.push(upload.upload(item.path, (res) => {
                                    this.setData({
                                        circleValue: res,
                                        showCircle: true
                                    })
                                }));
                            })
                            Promise.all(arr).then((res) => {
                                fileArr.forEach((item, index) => {
                                    item.address = res[index];
                                    item.name = upload.mathRand() + '.mp3';
                                    item.type = this.properties.type;
                                    item.resourceType = resourceType
                                    delete item.path
                                    delete item.size
                                    delete item.time
                                });
                                this.setData({
                                    circleValue: 0,
                                    showCircle: false,
                                })
                                this.triggerEvent('callSomeFun', fileArr)
                            });
                            this.setData({
                                showUploadEdit: false
                            })
                        }
                    })
                    break;
                case 4:
                    wx.chooseMessageFile({
                        type: 'file',
                        count: 2,
                        extension: ['pdf', 'ppt', 'xlsx', 'xls', 'docx', 'doc', 'pptx'],
                        success: (result) => {
                            let arr = []
                            let fileArr = result.tempFiles
                            fileArr.forEach(item => {
                                arr.push(upload.upload(item.path, (res) => {
                                    console.log(res);
                                    this.setData({
                                        circleValue: res,
                                        showCircle: true
                                    })
                                }));
                            })
                            Promise.all(arr).then((res) => {
                                fileArr.forEach((item, index) => {
                                    if (this.extname(item.path) == 'doc' || this.extname(item.path) == 'docx') {
                                        item.resourceType = 'word'
                                    } else if (this.extname(item.path) == 'ppt' || this.extname(item.path) == 'pptx') {
                                        item.resourceType = 'ppt'
                                    } else if (this.extname(item.path) == 'xlsx' || this.extname(item.path) == 'xls') {
                                        item.resourceType = 'excel'
                                    } else if (this.extname(item.path) == 'pdf') {
                                        item.resourceType = 'pdf'
                                    } else {
                                        item.resourceType = 'other'
                                    }
                                    item.address = res[index];
                                    item.name = upload.mathRand() + '.' + item.resourceType;
                                    item.type = this.properties.type;
                                    delete item.path
                                    delete item.size
                                    delete item.time
                                });
                                this.setData({
                                    circleValue: 0,
                                    showCircle: false,
                                })
                                this.triggerEvent('callSomeFun', fileArr)
                            });
                            this.setData({
                                showUploadEdit: false
                            })
                        }
                    })
                    break;
                default:
                    break;
            }
        },
        /** 隐藏上传操作dialog */
        onCloseUpload() {
            this.setData({
                showUploadEdit: false
            })
        },
    },
    ready: function () {

    }
})